//
//  FDAudioPlayerManager.swift
//  FDMediaKit
//
//  Created by Mac on 2020/6/10.
//
import Foundation
import AVFoundation

public enum FDAudioPlayerState {
    case playing
    case paused
    case stop
}

public class FDAudioPlayerManager : NSObject {
    
    public var playerState: FDAudioPlayerState = .stop
    
    private var player : AVPlayer?
    
    private var output: AVPlayerItemVideoOutput?
    
    public static let defaultManager: FDAudioPlayerManager = {
        let defaultManager = FDAudioPlayerManager()
        return defaultManager
    }()
    
    public func play(url : String) {
        let item = AVPlayerItem(url: URL(string: url)!)
        if player == nil {
            player = AVPlayer(playerItem: item)
        }else{
            player?.pause()
            player?.replaceCurrentItem(with: item)
        }
        output = AVPlayerItemVideoOutput()
        item.add(output!)
        player?.play()
        playerState = .playing
    }
    
    public func pause() {
        player?.pause()
        playerState = .paused
    }
    
    public func resume(){
        player?.play()
        playerState = .playing
    }
    
    public func stop() {
        player?.pause()
        playerState = .stop
    }
    
    public func startMatch(){
//        let timer = Timer.scheduledTimer(timeInterval: <#T##TimeInterval#>, target: <#T##Any#>, selector: <#T##Selector#>, userInfo: <#T##Any?#>, repeats: <#T##Bool#>)
//        
//        let itemTime = player?.currentItem?.currentTime()
//        
//        let pixelBuffer = output?.copyPixelBuffer(forItemTime: <#T##CMTime#>, itemTimeForDisplay: <#T##UnsafeMutablePointer<CMTime>?#>)
    }
    
}

